package com.thh.redisstudy.controller;

import com.thh.redisstudy.entity.SysUser;
import com.thh.redisstudy.mapper.SysUserMapper;
import com.thh.redisstudy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author kaizen
 * @since 2019-07-19
 **/
@RestController
public class TestConroller {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Resource
    private UserService userService;

    @RequestMapping("mapper")
    public String test() {
        SysUser sysUser = new SysUser();
        sysUser.setImage("image");
        sysUser.setUserName("kaizen");
        return String.valueOf(sysUserMapper.insert(sysUser));
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public SysUser getUser(Integer id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }


    @RequestMapping("/getByCache")
    @ResponseBody
    public SysUser getByCache(Integer id) {
        SysUser user = userService.findById(id);
        return user;
    }

    @ResponseBody
    @RequestMapping(value = "/getexpire", method = RequestMethod.GET)
    public SysUser findByIdTtl(Integer id) {
        SysUser u = new SysUser();
        try{
            u = userService.findByIdTtl(id);
        }catch (Exception e){
            System.err.println(e.getMessage());
        }
        return u;
    }


}
